package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.annotation.AllowAnonymous;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.system.domain.dto.CodeBuildRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.system.domain.query.SysCodeRuleQuery;
import com.jcfk.system.domain.dto.SysCodeRuleDTO;
import com.jcfk.system.domain.dto.SysCodeRoleItemDTO;
import com.jcfk.system.service.SysCodeRuleService;

import java.util.List;

/**
 * 编码生成规则
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-04-11
 */
@Api(tags = {"编码生成规则管理"})
@RestController
@RequestMapping("sysCodeRule")
public class SysCodeRuleController {
    @Autowired
    private SysCodeRuleService sysCodeRuleService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysCodeRuleQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询编码生成规则")
    @PostMapping("/queryByPage")
    public RetPage<SysCodeRuleDTO> queryByPage(@RequestBody ReqPageParam<SysCodeRuleQuery> requestParam) {
        DataPage<SysCodeRuleDTO> page = new DataPage<SysCodeRuleDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            SysCodeRuleDTO.class);
        page = sysCodeRuleService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysCodeRuleQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取编码生成规则列表", notes = "获取编码生成规则列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<SysCodeRoleItemDTO>> getDetails(@RequestBody SysCodeRuleQuery query) {

        List<SysCodeRoleItemDTO> list = sysCodeRuleService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysCodeRuleQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取编码生成规则列表", notes = "获取编码生成规则列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<SysCodeRuleDTO>> getList(@RequestBody SysCodeRuleQuery query) {

        List<SysCodeRuleDTO> list = sysCodeRuleService.getList(query);

        return RetResult.ok(list);
    }


    /**
     *  编码生成规则
     * 根据id 查询详情
     */
    @ApiOperation("查询编码生成规则")
    @GetMapping("/get/{id}")
    public RetResult<SysCodeRuleDTO> get(@PathVariable("id") String id) {
            SysCodeRuleDTO item = sysCodeRuleService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理编码生成规则
     */
    @ApiOperation("保存编码生成规则")
    @PostMapping("/save")
    @RequestLogHeader(modular = "编码生成规则管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysCodeRuleDTO sysCodeRule) {
        // 数据校验
        RetResult ret = sysCodeRuleService.isValid(sysCodeRule);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysCodeRule.getId())){
            // 新增
            sysCodeRuleService.insert(sysCodeRule);
        } else{
            // 更新
            sysCodeRuleService.update(sysCodeRule);
        }
        return RetResult.ok();
    }

    /**
     *  批量删除编码生成规则
     */
    @ApiOperation("批量删除编码生成规则")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "编码生成规则管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody SysCodeRuleDTO sysCodeRule) {
        return RetResult.ok(sysCodeRuleService.logicDeleteList(sysCodeRule));
    }

    @AllowAnonymous
    @PostMapping("/buildNewCode")
    public RetResult<?> buildNewCode(@RequestBody CodeBuildRequest buildRequest){
        String code=sysCodeRuleService.buildNewCode(buildRequest);
        return RetResult.ok(code);
    }

}
